<!--
 * @Author: your name
 * @Date: 2020-06-09 17:18:55
 * @LastEditTime: 2020-06-09 20:26:26
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \iem-web-projectd:\个人工具网站\www_admin_master\src\views\system\PageMsg.vue
-->
<template>
  <div class="uRow">
    <div class="col-left">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>页面路由权限管理</span>
        </div>
        <el-tree :data="data" :props="defaultProps" :expand-on-click-node="false" node-key="id">
          <span slot-scope="{ node, data }" class="custom-tree-node">
            <span>{{ data.name }}</span>
            <span>
              <el-button size="mini" type="text" @click="handleAddNode(node)">添 加</el-button>
              <el-button size="mini" type="text" @click="handleDetailsNode(node)">详情</el-button>
              <el-button size="mini" type="text" @click="handleDeleteNode(data)">删除</el-button>
              <el-button size="mini" type="text" class="el-icon-sort-up" />
              <el-button size="mini" type="text" class="el-icon-sort-down" />
            </span>
          </span>
        </el-tree>
        <el-button size="mini" type="text" @click="handleAddNode()">添 加</el-button>
      </el-card>
    </div>
    <div class="col-center" />
    <div class="col-right">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>详细信息</span>
        </div>
        <el-form :model="form">
          <el-form-item label="页面名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="路由名称">
            <el-input v-model="form.className" />
          </el-form-item>
          <el-form-item label="图标">
            <el-input v-model="form.icon" />
          </el-form-item>
          <el-form-item label="路由">
            <el-input v-model="form.router" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">确定</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { ADD_PAGE_INFO, FIND_ALL_PAGE_INFO, DELETE_ONE_USER_INFO } from '@/api/system'
export default {
  data () {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      form: {}
    }
  },
  mounted () {
    this.findPageAllInfo()
  },
  methods: {
    // 查询所有节点
    async findPageAllInfo () {
      const res = await this.$post(FIND_ALL_PAGE_INFO, {})
      const pageArr = [] // 一级
      for (const item of res) {
        if (item.parent_id === -1) {
          pageArr.push(item)
        }
      }
      for (const item of pageArr) {
        item.children = []
        for (const index of res) {
          if (index.parent_id === item.page_id) {
            item.children.push(index)
          }
        }
      }
      this.data = pageArr
    },
    // 添加节点信息
    handleAddNode (val) {
      this.form = {}
      if (val) {
        this.form.parent_id = val.data.page_id
        console.log(val)
      } else {
        return
      }
    },
    // 获得节点详情
    handleDetailsNode (val) {
      console.log('节点信息', val)
      this.form = val.data
    },
    // 删除节点信息
    async handleDeleteNode (val) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$post(DELETE_ONE_USER_INFO, { page_id: val.page_id }).then(() => {
          this.findPageAllInfo()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 提交节点信息
    async onSubmit () {
      const res = await this.$post(ADD_PAGE_INFO, this.form)
      console.log('提交信息', res)
      this.$message({
        message: res.msg,
        type: 'success'
      })
      this.findPageAllInfo()
    }
  }
}
</script>

<style lang="scss" scoped>
.uRow {
  width: 100%;
  .col-left {
    width: 25%;
    float: left;
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;
    }
  }
  .col-center {
    width: 1%;
    padding: 5px;
    box-sizing: border-box;
    float: left;
  }
  .col-right {
    width: 74%;
    float: left;
  }
}

</style>
